﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using DotNetNuke.Entities.Modules;
using DotNetNuke.UI.UserControls;
using DesertedRoadStudios.BlogNetNuke.Components;

namespace DesertedRoadStudios.BlogNetNuke
{
    public partial class WidgetsSettings : ModuleSettingsBase
    {
        // Widget Configuration
        DropDownList ddlParentBlog;
        DropDownList ddlWidget;
        RadioButtonList rblWidgetContainer;
        DropDownList ddlWidgetContainer;

        // Archives
        SectionHeadControl ArchivesSection;
        HtmlControl Archives;
        DropDownList ddlArchiveFormat;
        CheckBox cbArchivesShowPostCount;

        // Authors
        SectionHeadControl AuthorsSection;
        HtmlControl Authors;
        DropDownList ddlAuthorsDisplayedAs;
        DropDownList ddlAuthorsSortedBy;
        DropDownList ddlAuthorsSortOrder;
        CheckBox cbAuthorsShowPostCount;

        // Categories
        SectionHeadControl CategoriesSection;
        HtmlControl Categories;
        DropDownList ddlCategoriesSortedBy;
        DropDownList ddlCategoriesSortOrder;
        CheckBox cbCategoriesShowPostCount;

        // Tag Cloud
        SectionHeadControl TagCloudSection;
        HtmlControl TagCloud;
        TextBox tbTagsMaximum;

        // Latest Posts
        SectionHeadControl LatestPostsSection;
        HtmlControl LatestPosts;
        TextBox tbMaximumPosts;

        protected void Page_Init(object sender, EventArgs e)
        {
            // Widget Configuration
            ddlParentBlog = FindControl("ddlParentBlog") as DropDownList;
            ddlWidget = FindControl("ddlWidget") as DropDownList;
            rblWidgetContainer = FindControl("rblWidgetContainer") as RadioButtonList;
            ddlWidgetContainer = FindControl("ddlWidgetContainer") as DropDownList;

            // Archives
            ArchivesSection = FindControl("ArchivesSection") as SectionHeadControl;
            Archives = FindControl("Archives") as HtmlControl;
            ddlArchiveFormat = FindControl("ddlArchiveFormat") as DropDownList;
            cbArchivesShowPostCount = FindControl("cbArchivesShowPostCount") as CheckBox;

            // Authors
            AuthorsSection = FindControl("AuthorsSection") as SectionHeadControl;
            Authors = FindControl("Authors") as HtmlControl;
            ddlAuthorsDisplayedAs = FindControl("ddlAuthorsDisplayedAs") as DropDownList;
            ddlAuthorsSortedBy = FindControl("ddlAuthorsSortedBy") as DropDownList;
            ddlAuthorsSortOrder = FindControl("ddlAuthorsSortOrder") as DropDownList;
            cbAuthorsShowPostCount = FindControl("cbAuthorsShowPostCount") as CheckBox;

            // Categories
            CategoriesSection = FindControl("CategoriesSection") as SectionHeadControl;
            Categories = FindControl("Categories") as HtmlControl;
            ddlCategoriesSortedBy = FindControl("ddlCategoriesSortedBy") as DropDownList;
            ddlCategoriesSortOrder = FindControl("ddlCategoriesSortOrder") as DropDownList;
            cbCategoriesShowPostCount = FindControl("cbCategoriesShowPostCount") as CheckBox;

            // Tag Cloud
            TagCloudSection = FindControl("TagCloudSection") as SectionHeadControl;
            TagCloud = FindControl("TagCloud") as HtmlControl;
            tbTagsMaximum = FindControl("tbTagsMaximum") as TextBox;

            // Latest Posts
            LatestPostsSection = FindControl("LatestPostsSection") as SectionHeadControl;
            LatestPosts = FindControl("LatestPosts") as HtmlControl;
            tbMaximumPosts = FindControl("tbMaximumPosts") as TextBox;
        }

        protected void Page_Load(object sender, EventArgs e)
        {

        }

        public override void LoadSettings()
        {
            BlogNetNukeController bnn = new BlogNetNukeController();
            Dictionary<string, string> settings = bnn.GetSettings(PortalId, ModuleId);

            if (!IsPostBack)
            {
                List<BlogTab> blogs = bnn.GetAllBlogTabInfo(PortalId);
                foreach (BlogTab blog in blogs)
                    ddlParentBlog.Items.Add(new ListItem(blog.TabPath.Replace("//", "/"), blog.ModuleID.ToString()));
            }

            if ((settings.ContainsKey("ParentBlog")) && (ddlParentBlog.Items.FindByValue(settings["ParentBlog"]) != null))
                ddlParentBlog.SelectedValue = settings["ParentBlog"];

            if ((settings.ContainsKey("Widget")) && (ddlWidget.Items.FindByValue(settings["Widget"]) != null))
                ddlWidget.SelectedValue = settings["Widget"];

            ddlWidget_SelectedIndexChanged(this, null);

            if (settings.ContainsKey("WidgetContainerLocation") && settings.ContainsKey("WidgetContainerControl"))
            {
                rblWidgetContainer.SelectedValue = settings["WidgetContainerLocation"];
                rblWidgetContainer_SelectedIndexChanged(this, null);

                if (ddlWidgetContainer.Items.FindByValue(settings["WidgetContainerControl"].Substring(1)) != null)
                    ddlWidgetContainer.SelectedValue = settings["WidgetContainerControl"].Substring(1);
            }
            else
            {
                rblWidgetContainer.SelectedValue = "Samples";
                rblWidgetContainer_SelectedIndexChanged(this, null);
            }
        }

        public override void UpdateSettings()
        {
            BlogNetNukeController bnn = new BlogNetNukeController();
            bnn.ClearSettings(PortalId, ModuleId);
            bnn.SetSetting(PortalId, ModuleId, "ParentBlog", ddlParentBlog.SelectedValue);
            bnn.SetSetting(PortalId, ModuleId, "Widget", ddlWidget.SelectedValue);
            bnn.SetSetting(PortalId, ModuleId, "WidgetContainerLocation", rblWidgetContainer.SelectedValue);
            bnn.SetSetting(PortalId, ModuleId, "WidgetContainerControl", "~" + ddlWidgetContainer.SelectedValue);

            switch (ddlWidget.SelectedValue)
            {
                case "Archives":
                    bnn.SetSetting(PortalId, ModuleId, "ArchiveFormat", ddlArchiveFormat.SelectedValue);
                    bnn.SetSetting(PortalId, ModuleId, "ArchivesShowPostCount", cbArchivesShowPostCount.Checked.ToString());
                    break;
                case "Authors":
                    bnn.SetSetting(PortalId, ModuleId, "AuthorsSortedBy", ddlAuthorsSortedBy.SelectedValue);
                    bnn.SetSetting(PortalId, ModuleId, "AuthorsSortOrder", ddlAuthorsSortOrder.SelectedValue);
                    bnn.SetSetting(PortalId, ModuleId, "AuthorsShowPostCount", cbAuthorsShowPostCount.Checked.ToString());
                    break;
                case "Categories":
                    bnn.SetSetting(PortalId, ModuleId, "CategoriesSortedBy", ddlCategoriesSortedBy.SelectedValue);
                    bnn.SetSetting(PortalId, ModuleId, "CategoriesSortOrder", ddlCategoriesSortOrder.SelectedValue);
                    bnn.SetSetting(PortalId, ModuleId, "CategoriesShowPostCount", cbCategoriesShowPostCount.Checked.ToString());
                    break;
                case "TagCloud":
                    bnn.SetSetting(PortalId, ModuleId, "TagsMaximum", tbTagsMaximum.Text);
                    break;
                case "LatestPosts":
                    bnn.SetSetting(PortalId, ModuleId, "MaximumPosts", tbMaximumPosts.Text);
                    break;
            }
        }

        protected void rblWidgetContainer_SelectedIndexChanged(object sender, EventArgs e)
        {
            string[] files = { };
            string dirPrefix = string.Empty;
            string dirName = string.Empty;

            if (rblWidgetContainer.SelectedValue.Equals("Samples"))
                dirPrefix = MapPath("~/DesktopModules/BlogNetNuke/Containers");
            else if (rblWidgetContainer.SelectedValue.Equals("Host"))
                dirPrefix = MapPath("~/Portals/_default/Containers");
            else if (rblWidgetContainer.SelectedValue.Equals("Site"))
                dirPrefix = PortalSettings.HomeDirectoryMapPath + "Containers";

            ddlWidgetContainer.Items.Clear();

            try
            {
                files = Directory.GetFiles(dirPrefix, "*.ascx", SearchOption.AllDirectories);

                FileInfo fInfo;
                foreach (string file in files)
                {
                    fInfo = new FileInfo(file);
                    dirName = fInfo.FullName.Substring(dirPrefix.Length);
                    ddlWidgetContainer.Items.Add(new ListItem(dirName.Substring(1, dirName.Length - 1 - fInfo.Extension.Length).Replace("\\", " - "), fInfo.FullName.Substring(MapPath("~").Length - 1).Replace("\\", "/")));
                }
            }
            catch { }
        }

        protected void ddlWidget_SelectedIndexChanged(object sender, EventArgs e)
        {
            ArchivesSection.Visible = false;
            Archives.Visible = false;
            AuthorsSection.Visible = false;
            Authors.Visible = false;
            CategoriesSection.Visible = false;
            Categories.Visible = false;
            TagCloudSection.Visible = false;
            TagCloud.Visible = false;
            LatestPostsSection.Visible = false;
            LatestPosts.Visible = false;

            switch (ddlWidget.SelectedValue)
            {
                case "Archives":
                    ArchivesSection.Visible = true;
                    Archives.Visible = true;
                    LoadArchives();
                    break;
                case "Authors":
                    AuthorsSection.Visible = true;
                    Authors.Visible = true;
                    LoadAuthors();
                    break;
                case "BlogRoll":
                    LoadBlogRoll();
                    break;
                case "Calendar":
                    LoadCalendar();
                    break;
                case "Categories":
                    CategoriesSection.Visible = true;
                    Categories.Visible = true;
                    LoadCategories();
                    break;
                case "TagCloud":
                    TagCloudSection.Visible = true;
                    TagCloud.Visible = true;
                    LoadTagCloud();
                    break;
                case "LatestPosts":
                    LatestPostsSection.Visible = true;
                    LatestPosts.Visible = true;
                    LoadLatestPosts();
                    break;
            }
        }

        private void LoadArchives()
        {
            BlogNetNukeController bnn = new BlogNetNukeController();
            Dictionary<string, string> settings = bnn.GetSettings(PortalId, ModuleId);

            if (settings.ContainsKey("ArchiveFormat"))
                ddlArchiveFormat.SelectedValue = settings["ArchiveFormat"];

            if (settings.ContainsKey("ArchivesShowPostCount"))
                cbArchivesShowPostCount.Checked = Convert.ToBoolean(settings["ArchivesShowPostCount"]);
        }

        private void LoadAuthors()
        {
            BlogNetNukeController bnn = new BlogNetNukeController();
            Dictionary<string, string> settings = bnn.GetSettings(PortalId, ModuleId);

            if (settings.ContainsKey("AuthorsSortedBy"))
                ddlAuthorsSortedBy.SelectedValue = settings["AuthorsSortedBy"];

            if (settings.ContainsKey("AuthorsSortOrder"))
                ddlAuthorsSortOrder.SelectedValue = settings["AuthorsSortOrder"];

            if (settings.ContainsKey("AuthorsShowPostCount"))
                cbAuthorsShowPostCount.Checked = Convert.ToBoolean(settings["AuthorsShowPostCount"]);
        }

        private void LoadBlogRoll()
        {
        }

        private void LoadCalendar()
        {
        }

        private void LoadCategories()
        {
            BlogNetNukeController bnn = new BlogNetNukeController();
            Dictionary<string, string> settings = bnn.GetSettings(PortalId, ModuleId);

            if (settings.ContainsKey("CategoriesSortedBy"))
                ddlCategoriesSortedBy.SelectedValue = settings["CategoriesSortedBy"];

            if (settings.ContainsKey("CategoriesSortOrder"))
                ddlCategoriesSortOrder.SelectedValue = settings["CategoriesSortOrder"];

            if (settings.ContainsKey("CategoriesShowPostCount"))
                cbCategoriesShowPostCount.Checked = Convert.ToBoolean(settings["CategoriesShowPostCount"]);
        }

        private void LoadTagCloud()
        {
            BlogNetNukeController bnn = new BlogNetNukeController();
            Dictionary<string, string> settings = bnn.GetSettings(PortalId, ModuleId);

            if (settings.ContainsKey("TagsMaximum"))
                tbTagsMaximum.Text = settings["TagsMaximum"];
        }

        private void LoadLatestPosts()
        {
            BlogNetNukeController bnn = new BlogNetNukeController();
            Dictionary<string, string> settings = bnn.GetSettings(PortalId, ModuleId);

            if (settings.ContainsKey("MaximumPosts"))
                tbMaximumPosts.Text = settings["MaximumPosts"];
        }

    }
}